package com.shenmazong.demo1907amongo.controller;

import com.shenmazong.demo1907amongo.pojo.TbUser;
import com.shenmazong.demo1907amongo.service.ITbUserService;
import com.shenmazong.demo1907amongo.utils.ResultResponse;
import com.shenmazong.demo1907amongo.vo.LoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 军哥
 * @version 1.0
 * @description: TODO
 * @date 2021/12/8 10:52
 */

@RestController
@RequestMapping(value = "/user")
@Slf4j
@CrossOrigin
public class TbUserController {

    @Autowired
    ITbUserService iTbUserService;

    // 都在controller（接口）
    // url:http://localhost:port
    // api: 名字
    // param: 参数
    // return: 返回值
    // method: GET/POST/PUT/Delete

    @PostMapping(value = "/add")
    public ResultResponse add(@RequestParam("name") String  name, @RequestParam("pass") String pass) {

        TbUser tbUser = new TbUser();
        tbUser.setUserName(name);
        tbUser.setUserPass(pass);

        return iTbUserService.add(tbUser);
    }

    @PostMapping(value = "/getList")
    public ResultResponse getList(@RequestParam("pageSize") Integer  pageSize, @RequestParam("pageNum") Integer pageNum) {
        return iTbUserService.getList(pageSize, pageNum);
    }


    @PostMapping(value = "/getListByPage")
    public ResultResponse getListByPage(@RequestParam("pageSize") Integer  pageSize, @RequestParam("pageNum") Integer pageNum) {
        return iTbUserService.getListByPage(pageSize, pageNum);
    }

    @PostMapping(value = "/getListByPageVo")
    public ResultResponse getListByPageVo(@RequestParam("pageSize") Integer  pageSize, @RequestParam("pageNum") Integer pageNum) {
        return iTbUserService.getListByPageVo(pageSize, pageNum);
    }

    @PostMapping(value = "/login")
    public ResultResponse login(@RequestParam("username") String  username, @RequestParam("password") String password) {
        return iTbUserService.login(username, password);
    }

    // 1、所有接口都是用post方法（除非考试要求）
    // 2、参数的传递：@RequestBody
    // 3、返回值一律（ResultResponse：code,msg,flag,data）
    // 4、统一用mybatis plus的分页技术
    @PostMapping(value = "/loginByBody")
    public ResultResponse loginByBody(@RequestBody LoginVo loginVo) {
        return iTbUserService.login(loginVo.getName(), loginVo.getPass());
    }

}
